package com.gopay.cron.service.cloud.gen;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import com.gopay.cron.service.cloud.gen.bean.CloudRecvFileInfo;
import com.gopay.cron.service.cloud.gen.bean.CloudRefundFileInfo;
import com.gopay.cron.service.cloud.gen.bean.CloudSplitFileInfo;
import com.gopay.cron.service.cloud.gen.bean.CloudSplitRefundFileInfo;
import com.gopay.cron.web.vo.CloudAccountStatementVO;
import com.gopay.cron.web.vo.CloudSplitFileVO;
import com.gopay.cron.web.vo.CloudSplitRefundFileVO;

/**
 * @ClassName: EShopCloudOrderFileGenHelper 
 * @Description: E商通生成对账文件辅助类
 * @author liuxuyang
 * @date 2018年3月22日 上午9:39:11
 */
public class EShopCloudOrderFileGenHelper {

    private String filePath;
    private String prodCode;
    private String checkDate;
    private String acctId;  
    
    /**
     * 云账户收款类
     */
    private CloudRecvFileInfo cloudRecvFileInfo = new CloudRecvFileInfo();
    
    /*
     *云账户收款退款类
     */
    private CloudRefundFileInfo cloudRefundFileInfo = new CloudRefundFileInfo();
    
    /**
     * 云账户分账
     */
    private CloudSplitFileInfo cloudSplitFileInfo = new CloudSplitFileInfo();
    
    /**
     * 云账户分账退款
     */
    private CloudSplitRefundFileInfo cloudSplitRefundFileInfo = new CloudSplitRefundFileInfo();
    
    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getProdCode() {
        return prodCode;
    }

    public void setProdCode(String prodCode) {
        this.prodCode = prodCode;
    }

    public String getCheckDate() {
        return checkDate;
    }

    public void setCheckDate(String checkDate) {
        this.checkDate = checkDate;
    }

    public String getAcctId() {
        return acctId;
    }

    public void setAcctId(String acctId) {
        this.acctId = acctId;
    }

    private void writeFile(File file,String content) throws IOException {
        FileOutputStream fos = null;
        PrintWriter writer = null;
        try {
            fos = new FileOutputStream(file);
            writer = new PrintWriter(fos);
            writer.write(content);
            writer.flush();
        } finally {
            if(fos != null) {
                fos.close();
            }
            if(writer != null) {
                writer.close();
            }
        }
    }

    public void genFtpFile() throws IOException {
        File path = new File(getFilePath());
        if(!path.exists()) {
            path.mkdirs();
        }

        if("CLOUD".equals(getProdCode())) {
            //1.云账户收款类对账文件
            String recvFileName = getCheckDate() + "-" + getAcctId() + "-" + CloudRecvFileInfo.fileFtpName;
            File recvFile = new File(getFilePath() + recvFileName + ".csv");
            writeFile(recvFile, cloudRecvFileInfo.getFtpCsvContent());
            
            //2.云账户生成退款对账文件
            String refundFileName = getCheckDate() + "-" + getAcctId() + "-" + CloudRefundFileInfo.fileFtpName;
            File refundFile = new File(getFilePath() + refundFileName + ".csv");
            writeFile(refundFile, cloudRefundFileInfo.getFtpCsvContent());
            
            //3.云账户生成分账对账文件
            String splitFileName = getCheckDate() + "-" + getAcctId() + "-" + CloudSplitFileInfo.fileFtpName;
            File splitFile = new File(getFilePath() + splitFileName + ".csv");
            writeFile(splitFile, cloudSplitFileInfo.getFtpCsvContent());
            
            //4.云账户生成分账退款对账文件
            String splitRefundFileName = getCheckDate() + "-" + getAcctId() + "-" + CloudSplitRefundFileInfo.fileFtpName;
            File splitRefundFile = new File(getFilePath() + splitRefundFileName + ".csv");
            writeFile(splitRefundFile, cloudSplitRefundFileInfo.getFtpCsvContent());
        }
    }

    //FTP云账户收款类合计
    public void addFtpFirstRecvFileLineCloud(CloudAccountStatementVO vo) {
    	cloudRecvFileInfo.addFtpFirstRecvFileLine(vo);
    }
    
    //FTP云账户收款类明细
    public void addFtpSecondRecvFileLineCloud(CloudAccountStatementVO vo) {
    	cloudRecvFileInfo.addFtpSecondRecvFileLine(vo);
    }
    
    //FTP云账户收款退款类合计
    public void addFtpFirstRefundFileLineCloud(CloudAccountStatementVO vo) {
    	cloudRefundFileInfo.addFtpFirstRefundFileLine(vo);
    }
    
    //FTP云账户收款退款类明细
    public void addFtpSecondRefundFileLineCloud(CloudAccountStatementVO vo) {
    	cloudRefundFileInfo.addFtpSecondRefundFileLine(vo);
    }
    
    //FTP云账户分账类合计
    public void addFtpFirstSplitFileLineCloud(CloudAccountStatementVO vo) {
    	cloudSplitFileInfo.addFtpFirstSplitFileLine(vo);
    }
    //FTP云账户分账类明细
    public void addFtpSecondSplitFileLineCloud(CloudSplitFileVO vo) {
    	cloudSplitFileInfo.addFtpSecondSplitFileLine(vo);
    }
    
    //FTP云账户分账退款类合计
    public void addFtpFirstSplitRefundFileLineCloud(CloudAccountStatementVO vo) {
    	cloudSplitRefundFileInfo.addFtpFirstSplitRefundFileLine(vo);
    }
    
    //FTP云账户分账退款类明细
    public void addFtpSecondSplitRefundFileLineCloud(CloudSplitRefundFileVO vo) {
    	cloudSplitRefundFileInfo.addFtpSecondSplitRefundFileLine(vo);
    }
}
